<?php
class AreasController extends AppController {


	var $uses = array('Area');
	var $helpers = array('Access');


	function index() {
		$this->Area->recursive = 0;
		$this->set('areas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid area', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('area', $this->Area->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Area->create();
			if ($this->Area->save($this->data)) {
				$this->Session->setFlash(__('El area ha sido guardada', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El area no ha sido guardada por favor intente de nuevo.', true));
			}
		}
		$grados = $this->Area->Grado->find('list',array('fields'=>array('Grado.id','Grado.nombre')));
		$this->set(compact('grados'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid area', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Area->save($this->data)) {
				$this->Session->setFlash(__('El area ha sido guardada', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El area bi ha sido guardada por favor intente de nuevo.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Area->read(null, $id);
		}
		$grados = $this->Area->Grado->find('list',array('fields'=>array('Grado.nombre')));
		$this->set(compact('grados'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for area', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Area->delete($id)) {
			$this->Session->setFlash(__('Area eliminada', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Area no se puede eliminar', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function get_areas(){		
			$areas = $this->Area->find("list", array("fields"=>array("Area.id", "Area.nombre")));
			
			$this->view = 'Json';
			$this->set(compact('areas'));
			$this->set('json', 'areas');		
	}
	
	function get_areas_grado($grado = null){
		if( $grado != null ){
			$areas = $this->Area->find("list", array("fields"=>array("Area.id", "Area.nombre"), 
															  "conditions"=>array("Area.grado_id"=>$grado)));

			$this->view = 'Json';
			$this->set(compact('areas'));
			$this->set('json', 'areas');
		}
	}
}
